﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhuyenMaiVang.DAL;

namespace KhuyenMaiVang.Areas.Admin.Controllers
{ 
    public class PictureController : Controller
    {
        private KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();

        //
        // GET: /Admin/Picture/

        public ViewResult Index()
        {
            var pictures = db.Pictures.Include("Voucher");
            return View(pictures.ToList());
        }

        //
        // GET: /Admin/Picture/Create

        public ActionResult Create()
        {
            ViewBag.VoucherID = new SelectList(db.Vouchers, "ID", "Title");
            return View();
        } 

        //
        // POST: /Admin/Picture/Create

        [HttpPost]
        public ActionResult Create(Picture picture)
        {
            if (ModelState.IsValid)
            {
                db.Pictures.AddObject(picture);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.VoucherID = new SelectList(db.Vouchers, "ID", "Title", picture.VoucherID);
            return View(picture);
        }
       

        //
        // GET: /Admin/Picture/Delete/5
 
        public ActionResult Delete(int id)
        {
            Picture picture = db.Pictures.Single(p => p.ID == id);
            return View(picture);
        }

        //
        // POST: /Admin/Picture/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Picture picture = db.Pictures.Single(p => p.ID == id);
            db.Pictures.DeleteObject(picture);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}